/*
 * Copyright 2012-2025 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.assertj.core.error;

import static java.lang.reflect.Modifier.isPrivate;
import static java.lang.reflect.Modifier.isProtected;
import static java.lang.reflect.Modifier.isPublic;

import java.lang.reflect.Modifier;
import java.util.StringJoiner;

/**
 * Error message factory for an assertion which checks that a class has (or has not) a specific modifier.
 *
 * @author Michal Kordas
 */
public class ClassModifierShouldBe extends BasicErrorMessageFactory {

  private static final String PACKAGE_PRIVATE = "package-private";

  private ClassModifierShouldBe(Class<?> actual, boolean positive, String modifier) {
    super("%nExpecting actual:%n  %s%n" + (positive ? "to" : "not to") + " be a %s class but was %s.",
          actual, modifier, modifiers(actual));
  }

  /**
   * Creates a new instance for a positive check of the {@code final} modifier.
   *
   * @param actual the actual value in the failed assertion.
   * @return the created {@code ErrorMessageFactory}.
   */
  public static ErrorMessageFactory shouldBeFinal(Class<?> actual) {
    return new ClassModifierShouldBe(actual, true, Modifier.toString(Modifier.FINAL));
  }

  /**
   * Creates a new instance for a negative check of the {@code final} modifier.
   *
   * @param actual the actual value in the failed assertion.
   * @return the created {@code ErrorMessageFactory}.
   */
  public static ErrorMessageFactory shouldNotBeFinal(Class<?> actual) {
    return new ClassModifierShouldBe(actual, false, Modifier.toString(Modifier.FINAL));
  }

  /**
   * Creates a new instance for a positive check of the {@code public} modifier.
   *
   * @param actual the actual value in the failed assertion.
   * @return the created {@code ErrorMessageFactory}.
   */
  public static ErrorMessageFactory shouldBePublic(Class<?> actual) {
    return new ClassModifierShouldBe(actual, true, Modifier.toString(Modifier.PUBLIC));
  }

  /**
   * Creates a new instance for a positive check of the {@code protected} modifier.
   *
   * @param actual the actual value in the failed assertion.
   * @return the created {@code ErrorMessageFactory}.
   */
  public static ErrorMessageFactory shouldBeProtected(Class<?> actual) {
    return new ClassModifierShouldBe(actual, true, Modifier.toString(Modifier.PROTECTED));
  }

  /**
   * Creates a new instance for a positive check of the {@code package-private} modifier.
   *
   * @param actual the actual value in the failed assertion.
   * @return the created {@code ErrorMessageFactory}.
   */
  public static ErrorMessageFactory shouldBePackagePrivate(Class<?> actual) {
    return new ClassModifierShouldBe(actual, true, PACKAGE_PRIVATE);
  }

  /**
   * Creates a new instance for a positive check of the {@code private} modifier.
   *
   * @param actual the actual value in the failed assertion.
   * @return the created {@code ErrorMessageFactory}.
   */
  public static ErrorMessageFactory shouldBePrivate(Class<?> actual) {
    return new ClassModifierShouldBe(actual, true, Modifier.toString(Modifier.PRIVATE));
  }

  /**
   * Creates a new instance for a positive check of the {@code static} modifier.
   *
   * @param actual the actual value in the failed assertion.
   * @return the created {@code ErrorMessageFactory}.
   */
  public static ErrorMessageFactory shouldBeStatic(Class<?> actual) {
    return new ClassModifierShouldBe(actual, true, Modifier.toString(Modifier.STATIC));
  }

  /**
   * Creates a new instance for a negative check of the {@code static} modifier.
   *
   * @param actual the actual value in the failed assertion.
   * @return the created {@code ErrorMessageFactory}.
   */
  public static ErrorMessageFactory shouldNotBeStatic(Class<?> actual) {
    return new ClassModifierShouldBe(actual, false, Modifier.toString(Modifier.STATIC));
  }

  private static String modifiers(Class<?> actual) {
    int modifiers = actual.getModifiers();
    boolean isPackagePrivate = !isPublic(modifiers) && !isProtected(modifiers) && !isPrivate(modifiers);
    String modifiersDescription = toString(modifiers);
    StringJoiner sj = new StringJoiner(" ");

    if (isPackagePrivate) {
      sj.add(PACKAGE_PRIVATE);
    }
    if (!modifiersDescription.isEmpty()) {
      sj.add(modifiersDescription);
    }

    return sj.toString();
  }

  /**
   * Return a string describing the access modifier flags in the specified modifiers,
   * filtering out the {@link Modifier#SYNCHRONIZED SYNCHRONIZED},
   * {@link Modifier#STRICT STRICT}, and {@link Modifier#VOLATILE VOLATILE} bits as
   * Valhalla's {@link Modifier#toString(int)}} mis-interprets them.
   *
   * @param modifiers a set of modifiers
   * @return a string representation of the set of modifiers, with {@code SYNCHRONIZED},
   * {@code STRICT}, and {@code VOLATILE} filtered out
   *
   * @see Modifier#toString(int)
   * @see <a href="https://bugs.openjdk.org/browse/JDK-8370935">JDK-8370935</a>
   * @see <a href="https://github.com/openjdk/valhalla/blob/296fe862f73ad92093d62372141dc848a3e42d72/src/java.base/share/classes/java/lang/Class.java#L326-L328">Class.java#L326-L328</a>
   */
  private static String toString(int modifiers) {
    return Modifier.toString(modifiers & ~(Modifier.SYNCHRONIZED | Modifier.STRICT | Modifier.VOLATILE));
  }

}
